package com.nbjtjc.safe.api.cost;

import cn.hutool.core.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.nbjtjc.safe.annotation.SysLog;
import com.nbjtjc.safe.bean.Result;
import com.nbjtjc.safe.model.cost.CostAccountRecord;
import com.nbjtjc.safe.model.cost.CostBudgetPlan;
import com.nbjtjc.safe.service.cost.CostAccountRecordService;

@RestController
@CrossOrigin("*")
@RequestMapping("/costAccountRecord")
public class CostAccountRecordController {
	@Autowired
	private CostAccountRecordService costAccountRecordService;


	  @RequestMapping(value = "/addOrUpdate",method = {RequestMethod.POST})
	  @SysLog("添加或更新安全投入预算计划项目")
	    public Result add(@RequestBody CostAccountRecord costAccountRecord){
	        Result result=new Result();
	        try {
	            result=costAccountRecordService.add(costAccountRecord);
	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse(e.getMessage());
	        }
	        return result;
	    }

	  
	  @RequestMapping(value = "/delete",method = {RequestMethod.POST})
	  @SysLog("删除安全投入预算计划项目")
	    public Result delete(@RequestBody CostAccountRecord costAccountRecord) {
	        Result result=new Result();
	        try {
	            result=costAccountRecordService.delete(costAccountRecord);
	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse(e.getMessage());
	        }
	        return result;
	    }

	  @RequestMapping(value = "/find", method = {RequestMethod.POST})
	  public Result find(@RequestBody CostAccountRecord costAccountRecord) {
	        Result result = new Result();

	        try {
	            result = costAccountRecordService.findByRegId(costAccountRecord);
	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse(e.getMessage());
	        }
	        return result;
	    }
	  
	  @RequestMapping(value = "/updateAllItemAccount", method = {RequestMethod.POST})
	  public Result updateAllItemAccount(@RequestBody CostBudgetPlan costBudgetPlan) {
	        Result result = new Result();

	        try {
	        	if(ObjectUtil.isNull(costBudgetPlan)||ObjectUtil.isNull(costBudgetPlan.getId())){
//	        		result.setFalse("缺少预算计划Id，无法更新");
	        		return result;
	        	}
	            result = costAccountRecordService.updateAllItemAccount(costBudgetPlan.getId());
	        } catch (Exception e) {
	            e.printStackTrace();
	            result.setFalse(e.getMessage());
	        }
	        return result;
	    }
	  
}
